package com.jack.leetcode.eachProblem07;

/**
 * Created by zhang_j on 2020/7/10
 */
public class 最佳买卖股票时机含冷冻期 {

    public static void main(String[] args) {
        int[] a = {1,2,3,0,2};
        System.out.println(maxProfit(a));
        int[] b = {2,1,4};
        System.out.println(maxProfit(b));
    }

    public static int maxProfit(int[] prices) {
        int n = prices.length;
        if(n < 2){
            return 0;
        }
        int[] hold = new int[n];
        int[] unhold = new int[n];
        hold[0] = -prices[0];
        unhold[0] = 0;

        for(int i = 1;i < n;i++){
            if(i == 1){
                hold[i] = Math.max(hold[0], -prices[i]);
            }else{
                hold[i] = Math.max(hold[i-1], unhold[i-2]-prices[i]);
            }
            unhold[i] = Math.max(hold[i-1] + prices[i], unhold[i-1]);
        }
        return unhold[n-1];
    }

}
